# -*- coding: utf8 -*-
import logging
import re
import common

site_index = 'lyricwiki'
site_keyword = 'lyrics.wikia'
site_url = 'http://lyrics.wikia.com/'
test_url = u'http://lyrics.wikia.com/ナイトメア_(Nightmare):レゾンデートル'.encode('utf8')
# test_url = 'http://lyrics.wikia.com/Ronan_Keating:When_You_Say_Nothing_At_All'

def get_lyric(url):
    encoding = 'utf8'

    bytes = common.get_url_content(url)
    html = bytes.decode(encoding, 'ignore')

    pattern = 'wgTitle="([^"]+)"'
    wgTitle = common.get_first_group_by_pattern(html, pattern)
    logging.debug(repr(wgTitle))

    artist = ''
    title = ''
    infos = wgTitle.split(':')
    if len(infos) >= 2:
        artist = infos[0]
        title = infos[1]

    pattern = '</div>(&#.*)<!--'
    items = re.findall(pattern, html)
    logging.debug(repr(items))

    lyrics = []
    for item in items:
        string = item.replace('<br />', '\n')

        string += '\n'
        lyrics.append(string)

    lyric = u'%s\n\nArtist: %s\n\n\n%s' % (title, artist, '\n\n'.join(lyrics))

    # convert &#115; to unicode character
    def code_point_to_char(matchobj):
        if matchobj.group(1):
            return unichr(int(matchobj.group(1)))
    pattern = '&#([0-9]+);'
    lyric = re.sub(pattern, code_point_to_char, lyric)

    return lyric

if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)
    url = test_url
    print url
    lyric = get_lyric(url)
    print repr(lyric) 
